/**
 * Created by Administrator on 2015/10/20.
 */

Date.prototype.Format = function(fmt) { //author: meizz
      var o = {
                "M+" : this.getMonth()+1,                 //月份
                "d+" : this.getDate(),                    //日
                "h+" : this.getHours(),                   //小时
                "m+" : this.getMinutes(),                 //分
                "s+" : this.getSeconds(),                 //秒
                "S"  : this.getMilliseconds(),             //毫秒
                "TT" : this.getHours() < 12 ? "AM" : "PM",
              };
    if (/(y+)/.test(fmt)){
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "")
                .substr(4 - RegExp.$1.length));
    }
    for ( var k in o){
        if (new RegExp("(" + k + ")").test(fmt)){
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k])
                    : (("00" + o[k]).substr(("" + o[k]).length)));
        };
    }
    return fmt;
};

/**
 * yyyy-MM-dd HH:MM:SS
 * @param seperator1 “-”
 * @param seperator2 “:”
 * @param seperator3 空格
 * @returns {*}
 */
function getDate(seperator1,seperator2,seperator3)
{
  var date = new Date();
  //var seperator1 = "-";
  //var seperator2 = ":";
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
    + seperator3 + date.getHours() + seperator2 + date.getMinutes()
    + seperator2 + date.getSeconds();
  return currentdate;
}
function trimStr(str){return str.replace(/(^\s*)|(\s*$)/g,"");}
//根据民族查询对应的民族编码
/**
 * @desc 民族编号获取
 * @author xiongxl
 * @since 2016-01-18
 */
function getFolkCode(pNation){
  var folkCode = pNation;

  switch (pNation) {
    case "汉族"       : folkCode = '01'; break;
    case "回族"       : folkCode = '02'; break;
    case "满族"       : folkCode = '03'; break;
    case "壮族"       : folkCode = '04'; break;
    case "白族"       : folkCode = '05'; break;
    case "蒙古族"     : folkCode = '06'; break;
    case "布依族"     : folkCode = '07'; break;
    case "哈萨克族"   : folkCode = '08'; break;
    case "苗族"       : folkCode = '09'; break;
    case "京族"       : folkCode = '10'; break;
    case "维吾尔族"   : folkCode = '11'; break;
    case "藏族"       : folkCode = '12'; break;
    case "傣族"       : folkCode = '13'; break;
    case "瑶族"       : folkCode = '14'; break;
    case "土家族"     : folkCode = '15'; break;
    case "朝鲜族"     : folkCode = '16'; break;
    case "赫哲族"     : folkCode = '17'; break;
    case "达斡尔族"   : folkCode = '18'; break;
    case "鄂伦春族"   : folkCode = '19'; break;
    case "鄂温克族"   : folkCode = '20'; break;
    case "东乡族"     : folkCode = '21'; break;
    case "保安族"     : folkCode = '22'; break;
    case "撤拉族"     : folkCode = '23'; break;
    case "土族"       : folkCode = '24'; break;
    case "裕固族"     : folkCode = '25'; break;
    case "俄罗斯族"   : folkCode = '26'; break;
    case "乌孜别克族" : folkCode = '27'; break;
    case "塔塔尔族"   : folkCode = '28'; break;
    case "锡伯族"     : folkCode = '29'; break;
    case "柯尔克孜族" : folkCode = '30'; break;
    case "塔吉克族"   : folkCode = '31'; break;
    case "门巴族"     : folkCode = '32'; break;
    case "珞巴族"     : folkCode = '33'; break;
    case "纳西族"     : folkCode = '34'; break;
    case "彝族"       : folkCode = '35'; break;
    case "普米族"     : folkCode = '36'; break;
    case "僳僳族"     : folkCode = '37'; break;
    case "怒族"       : folkCode = '38'; break;
    case "独龙族"     : folkCode = '39'; break;
    case "景颇族"     : folkCode = '40'; break;
    case "阿昌族"     : folkCode = '41'; break;
    case "德昂族"     : folkCode = '42'; break;
    case "佤族"       : folkCode = '43'; break;
    case "拉祜族"     : folkCode = '44'; break;
    case "布朗族"     : folkCode = '45'; break;
    case "基诺族"     : folkCode = '46'; break;
    case "哈尼族"     : folkCode = '47'; break;
    case "黎族"       : folkCode = '48'; break;
    case "毛南族"     : folkCode = '49'; break;
    case "仫佬族"     : folkCode = '50'; break;
    case "侗族"       : folkCode = '51'; break;
    case "水族"       : folkCode = '52'; break;
    case "仡佬族"     : folkCode = '53'; break;
    case "羌族"       : folkCode = '54'; break;
    case "畲族"       : folkCode = '55'; break;
    case "高山族"     : folkCode = '56'; break;
    case "其他族"     : folkCode = '57'; break;
    case "外国血统，中国籍人士族": folkCode = '58'; break;
  }
  return folkCode;
}


//封包加解密
function encryptByDES(message, key) {
  // For the key, when you pass a string,
  // it's treated as a passphrase and used to derive an actual key and IV.
  // Or you can pass a WordArray that represents the actual key.
  // If you pass the actual key, you must also pass the actual IV.
  var keyHex = CryptoJS.enc.Utf8.parse(key);
  //console.log(CryptoJS.enc.Utf8.stringify(keyHex), CryptoJS.enc.Hex.stringify(keyHex));
  //console.log(CryptoJS.enc.Hex.parse(CryptoJS.enc.Utf8.parse(key).toString(CryptoJS.enc.Hex)));

  // CryptoJS use CBC as the default mode, and Pkcs7 as the default padding scheme
  var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  });
  // decrypt encrypt result
  // var decrypted = CryptoJS.DES.decrypt(encrypted, keyHex, {
  //     mode: CryptoJS.mode.ECB,
  //     padding: CryptoJS.pad.Pkcs7
  // });
  // console.log(decrypted.toString(CryptoJS.enc.Utf8));

  // when mode is CryptoJS.mode.CBC (default mode), you must set iv param
  // var iv = 'inputvec';
  // var ivHex = CryptoJS.enc.Hex.parse(CryptoJS.enc.Utf8.parse(iv).toString(CryptoJS.enc.Hex));
  // var encrypted = CryptoJS.DES.encrypt(message, keyHex, { iv: ivHex, mode: CryptoJS.mode.CBC });
  // var decrypted = CryptoJS.DES.decrypt(encrypted, keyHex, { iv: ivHex, mode: CryptoJS.mode.CBC });

  // console.log('encrypted.toString()  -> base64(ciphertext)  :', encrypted.toString());
  // console.log('base64(ciphertext)    <- encrypted.toString():', encrypted.ciphertext.toString(CryptoJS.enc.Base64));
  // console.log('ciphertext.toString() -> ciphertext hex      :', encrypted.ciphertext.toString());
  return encrypted.toString();
}

/**
 * Decrypt ciphertext by DES in ECB mode and Pkcs7 padding scheme
 *
 * @param  {String} ciphertext(base64 string)
 * @param  {String} key
 * @return {String} plaintext
 *
 * @author Sun
 * @version 2013-5-15
 */
function decryptByDES(ciphertext, key) {
  var keyHex = CryptoJS.enc.Utf8.parse(key);

  // direct decrypt ciphertext
  var decrypted = CryptoJS.DES.decrypt({
    ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
  }, keyHex, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  });

  return decrypted.toString(CryptoJS.enc.Utf8);
}
